/**
 * 
 */
package com.dhcc.android.base.utils.dataparse;

import android.util.Xml;

import com.dhcc.android.base.common.bean.BasicProperties;
import com.dhcc.android.base.common.bean.Item;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务包下的config.xml解析文件
 * @author pengling
 * @date 2015-5-24
 */
public class PhotoParse {
	/**
	 * 解析XML
	 * 
	 * @return 返回Object
	 */
	public BasicProperties getXmlObject(InputStream is, BasicProperties photo) {
		PullParse pullParse=new PullParse();
		XmlPullParser parser = Xml.newPullParser();
		List<Item> watermarkList = null;
		List<Item> labelList = null;
		Item watermark=null;
		Item label=null;
		try {
			parser.setInput(is, "UTF-8");
			// 事件类型
			int eventType = parser.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
//					photo = new BasicProperties();
					break;
				case XmlPullParser.START_TAG:
					// 获得当前节点元素的名称
					String nodeName = parser.getName();
					if ("NAME".equalsIgnoreCase(nodeName)) {
						pullParse.setChildParam(parser, photo);
					} else if ("LABELS".equalsIgnoreCase(nodeName)) {
						if (labelList == null) {
							labelList = new ArrayList<Item>();
						}
//						itemList = new ArrayList<Item>();
						// 判断标签里是否有属性，如果有，则全部解析出来
						pullParse.setChildParam(parser, label);
					} else if ("ITEM".equalsIgnoreCase(nodeName)) {
						if(photo.getLabels()==null||photo.getLabels().size()==0){
							label = new Item();
							pullParse.setChildParam(parser, label);
							if(labelList!=null){
								labelList.add(label);
							}
						}else{
							watermark=new Item();
							pullParse.setChildParam(parser, watermark);
							if(watermarkList!=null){
								watermarkList.add(watermark);
							}
						}
					} else if ("PATH".equalsIgnoreCase(nodeName)) {
						pullParse.setChildParam(parser, photo);
					}else if ("WATERMARK".equalsIgnoreCase(nodeName)) {
						if (watermarkList == null) {
							watermarkList = new ArrayList<Item>();
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if ("LABELS".equalsIgnoreCase(parser.getName())) {
						photo.setLabels(labelList);
					}
					if ("WATERMARK".equalsIgnoreCase(parser.getName())) {
						photo.setWatermark(watermarkList);
					}
				}
				eventType = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return photo;
	}

}
